package indi.kider.server.bill.service;

import indi.kider.common.model.bill.RentInfoDay;
import indi.kider.server.bill.mapper.RentInfoDayMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@SuppressWarnings("ALL")
@Service
@Slf4j
public class RentInfoEveryDayImpl implements RentInfoEveryDay {

    @Autowired
    private RentInfoDayMapper rentInfoDayMapper;

    @Override
    public int extractRentInfo() {
        //todo 未考虑邀请好友的情况
        // 判断当天是否已经提取过，提取过，不让提取

        try {
            LocalDate date = LocalDate.now();
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String day = date.format(fmt);

            Example example = new Example(RentInfoDay.class);
            example.createCriteria().andEqualTo("day", day);
            List<RentInfoDay> list = rentInfoDayMapper.selectByExample(example);
            log.info("判断当天【{}】已经统计过租住情况总数【{}】", day, list.size());
            if (null == list || list.isEmpty()){
                log.info("【开始统计过租住情况】");
                rentInfoDayMapper.extractRentInfo();
            }

        }catch (Exception e){
            log.debug("提取当天的租住情况出现错误【{}】", e.getMessage());
            return 1;
        }

        return 0;
    }

}
